package com.ruoyi.api.rabbitMQ;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * @Author yinwenhao
 * @Date 2020-11-26
 * @desc 发送者  -- 生产延时消息
 */
@Component
public class ProduceDelayOrder{

   @Autowired
   private RabbitTemplate rabbitTemplate;

    public void sendMessage(String msg,long time){
        /*
        * 参数1: 发送的交换机
        * 参数2: 路由key
         * 参数3: 发送的消息
         * 参数4: 消息延时
         * */
        rabbitTemplate.convertAndSend(RabbitmqConfig.ORDER_DELAY_EXCHANGE,RabbitmqConfig.ROUTINGKEY,msg,message -> {
            // 注意这里时间可以使long，而且是设置header
            message .getMessageProperties().setHeader("x-delay",time);
            return message;
        });
       System.out.println("发送延时"+time+"毫秒的消息为:"+msg);
   }

}
